﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 汽车信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarController : ControllerBase
    {

        /// <summary>
        ///  增加汽车信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(CarRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Carinfo info = new Carinfo();
                info.CarId = obj.CarId;
                info.CarName = obj.CarName;
                info.CarNo = obj.CarNo;
                info.Brand = obj.Brand;
                info.AliasName = obj.AliasName;
                info.DeliveryTime = obj.DeliveryTime;
                info.CreateTime = obj.CreateTime ?? DateTime.Now;
                info.IsEnable = obj.IsEnable ?? 1;
                result.Status = CarInfoBLL.Instance.Add(info);
                if (result.Status == -2)
                {
                    result.Status = -2;
                    result.Message = "CarNo Is Exist";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Car/Add Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }



        /// <summary>
        ///  编辑汽车信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Update(CarRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Carinfo info = new Carinfo();
                info.CarId = obj.CarId;
                info.CarName = obj.CarName;
                info.CarNo = obj.CarNo;
                info.Brand = obj.Brand;
                info.AliasName = obj.AliasName;
                info.DeliveryTime = obj.DeliveryTime;
                info.CreateTime = obj.CreateTime ?? DateTime.Now;
                info.IsEnable = obj.IsEnable ?? 1;
                result.Status = CarInfoBLL.Instance.Update(info);
                if (result.Status == -1)
                {
                    result.Message = "CarInfo Is Not Exist";
                }
                if (result.Status == -2)
                {
                    result.Message = "CarNo Already Occupied";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Car/Update Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  删除汽车信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Remove(RemoveCarRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                result.Status = CarInfoBLL.Instance.Remove(obj.CarId);
                if (result.Status == -1)
                {
                    result.Message = "CarInfo Is Not Exist";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Car/Remove Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }



        /// <summary>
        ///  获取汽车信息 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetByPage(CarShearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                result.FirstParam= CarInfoBLL.Instance.GetByPage(obj.IsEnable,  page);
                result.SecondParam = page.TotalRows;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Car/GetByPage Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

    }
}